<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>品牌列表</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<link rel="stylesheet" href="__PEAR__/admin/css/other/department.css"/>
	</head>
	<body class="pear-container">

		<div class="layui-row layui-col-space15">
			<div class="layui-col-md3">
				<div class="layui-card">
					<div class="layui-card-body">
						<div id="basecodeTreeContent" style="overflow: auto">
							<ul id="basecodeTree" class="dtree organizationTree"></ul>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-col-md9">
				<div class="layui-card">
					<div class="layui-card-body">
						<table id="basecode-table" lay-filter="basecode-table"></table>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="basecode-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>

		<script type="text/html" id="basecode-bar">
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
				class="layui-icon layui-icon-delete"></i></button>
	    </script>
		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script src="__STATIC__/js/admin/common.js"></script>
		<script>
			var type_no="";
			var type_name="";
			
			layui.use(['table', 'form', 'jquery', 'dtree'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let dtree = layui.dtree;
				
				type_no='';
				type_name='';
				
				let MODULE_PATH = "/{$MODULE_NAME}/Basecode/";
				let TREE_PATH=MODULE_PATH+"type?data=tree_json";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '编号',
							field: 'code_id',
							align: 'center'
						},
						{
							title: '名称',
							field: 'code_name',
							align: 'center'
						},
						{
							title: '英文名称',
							field: 'english_name',
							align: 'center'
						},
						{
							title: '代码分类',
							field: 'code_type',
							align: 'center'
						},
						{
							title: '备注',
							field: 'memo',
							align: 'center'
						},
						{
							title: '操作',
							toolbar: '#basecode-bar',
							align: 'center',
							width: 130
						}
					]
				]
				var treeHeight=parent.globalHeight-25;
				var DTree = dtree.render({
					elem: "#basecodeTree",
					height:treeHeight,
					initLevel: "2", //默认展开层级为1
					line: true, // 有线树
					ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
					icon: ["0", "0"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
					method: 'get',
					url: TREE_PATH
				});

				table.render({
					elem: '#basecode-table',
					url: MODULE_PATH+'codelist?data=json&type_no=pp',
					height: 'full-50',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#basecode-toolbar',
					defaultToolbar: []
				});

				// 绑定节点点击事件
				dtree.on("node(basecodeTree)", function(obj) {
					type_name=obj.param.context;
					type_no=obj.param.nodeId;
					
					//console.log(obj.param.nodeId);
					if (!obj.param.leaf) {
						var $div = obj.dom;
						DTree.clickSpread($div); //调用内置函数展开节点
					} else {
						//layer.msg("正在获取数据");
						table.reload("basecode-table",{
							where:{'type_no':type_no},
							page:{curr:1}
						});
					}
				});

				table.on('tool(basecode-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(basecode-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(basecode-query)', function(data) {
					table.reload('basecode-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});

				window.add = function() {
					if(type_no==''||type_no==0){
						layer.msg("请从左边分类树选择分类");
						return;
					}
					layer.open({
						type: 2,
						title: '新增'+type_name+"基础代码",
						shade: 0.1,
						area: ['500px', '400px'],
						content: MODULE_PATH + 'add/type/'+type_no+'.html'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该代码?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "del/type_no/" + obj.data['type_no']+"/code_id/"+obj.data['code_id'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.batchRemove = function(obj) {
					let data = table.checkStatus(obj.config.id).data;
					if (data.length === 0) {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					let code_id = "";
					let typeno="";
					for (let i = 0; i < data.length; i++) {
						code_id += data[i].code_id + ",";
						typeno += data[i].type_no + ",";
					}
					code_id = code_id.substr(0, code_id.length - 1);
					typeno = typeno.substr(0, typeno.length - 1);
					
					var obj=new Object();
						obj.code_id=code_id;
						obj.type_no=typeno;
			
					layer.confirm('确定要删除这些代码', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "batchDel",
							dataType: 'json',
							type: 'delete',
							data:obj,
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('basecode-table',{
											where:{'type_no':type_no},
											page:{curr:1}
										});
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}
				
				//$(window).resize(function(){
					//var treeHeight=getParentWinHeight($)-25;
					//rebuildTree(dtree,"basecodeTree",TREE_PATH,treeHeight);
				//})
				
			})
		</script>
	</body>
</html>
